

import 'package:oral_cloud/config/net/api.dart';
import 'package:oral_cloud/config/net/oral_api.dart';
import 'package:oral_cloud/model/medical_imaging.dart';
import 'package:oral_cloud/model/patient.dart';
import 'package:oral_cloud/model/user.dart';
import 'package:oral_cloud/view_model/login_model.dart';

class OralRepository {
  static const bool isTest=false;
  static Future login(String username,String pwdOrVCode,int loginType) async{
    if(isTest){
      return loginOfTest(username, pwdOrVCode);
    }else{
      if(loginType==LoginModel.LoginTypeVCode) {
        return OralRepository.loginOfVCode(username, pwdOrVCode);
      }else{
        return OralRepository.loginOfPassword(username, pwdOrVCode);
      }
    }

  }
  static Future loginOfTest(String username,String password) async{
    return Future.delayed(new Duration(milliseconds: 1000), () {

      var data ={
        "code": 200,
        "msg": "success",
        "data": {
          "id": 1,
          "username": "useraccount",
          "mobile": "15551053111",
          "sex": 1,
          "education": "硕士",
          "birthDate": "2019-07-03",
          "address": "安徽省",
          "doctorTitle": "主任",
          "clinicName": "安医大",
          "avatar": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596013778401&di=a0cd8bbe4de9c87e6d8348c0bd9de890&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2F5%2Ffb%2Facc1823239.jpg",
          "expertise": [
            "耳鼻喉"
          ],
          "status": 2,
          "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjbGluaWNJZCI6IjEiLCJyb2xlSWQiOiIzIiwiZXhwIjoxNTk4NDI4NTc3LCJ1c2VySWQiOiIxIiwidXNlcm5hbWUiOiJ1c2VyYWNjb3VudCJ9.w83yLp1P6hGtKbtiMhDixCAo-Awkb6SThYpnz7Xsuhw",
          "doctorCertificateVO": {
            "qualificationNo": "AHHF102355200000",
            "certifiedNo": "AHHF102355200000",
            "qualificationPic": ["https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598862701985&di=17863d8f294137f0d91188ae30e10b4b&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2F1%2F22%2F7cb41452684.jpg","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598951417169&di=8362ff79a0c43775e2adb183a6168e41&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2Fb%2Fb5%2F41e574527.jpg","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598951533078&di=5cf3ad3063ce2e8bcf87e7ca45c72049&imgtype=0&src=http%3A%2F%2Fpic.feizl.com%2Fupload%2Fallimg%2F170905%2F0525nevsimf0cgx.jpg"],
            "certifiedPic": ["https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598951417168&di=cb242d103850213f2076d6869acf4339&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2Fd%2Fa2%2Fc15a426417.jpg","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598951417165&di=ba3cb138bc1ed75234bad4278488fa4e&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2F6%2Fa6%2F39b2375640.jpg","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598951424282&di=db8a3e9c0f843cd593f2ef71bf429d27&imgtype=0&src=http%3A%2F%2Fy1.ifengimg.com%2Fnews_spider%2Fdci_2013%2F04%2Ffba825cf71a2d9640cbb3e2034581d7d.jpg"],
            "titlePic": ["https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598951533054&di=9c746e0d7a41ce9c2c97d68794065ad0&imgtype=0&src=http%3A%2F%2Fpics1.baidu.com%2Ffeed%2F32fa828ba61ea8d3121306f7d25b0748241f58e3.jpeg%3Ftoken%3D1208cfb637f644133d1af69ec8488ccf%26s%3D1B020DCB8CB2009CB329618A0300A0D2","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598951533049&di=97689c3e8ef189787541d591865d2379&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fmobile%2Fe%2F535495b29e279.jpg","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598951533079&di=99e50b2210aa2e4056c2e6116de40930&imgtype=0&src=http%3A%2F%2Fimg.tupianzj.com%2Fuploads%2Fallimg%2F160414%2F9-1604140T934.jpg"]
          },
          "role": "doctor"
        }
      };

      ResponseData response=ResponseData.fromJson(data);
      return User.fromJsonMap(response.data);
    });
  }


  //用户名+密码登录
  static Future loginOfPassword(String username,String password) async{
    print("loginOfPassword->username=$username,password=$password");
    var response =await http.post('doctor/login',data: {
      'username':username,
      'password':password,
      'roleType':'doctor',
    },options: Options(contentType: Headers.jsonContentType));
    print("loginOfPassword->response=$response");
    return User.fromJsonMap(response.data);
  }

  //手机号+验证码登录
  static Future loginOfVCode(String mobile,String vcode) async{
    print("loginOfVCode->mobile=$mobile ,vcode=$vcode");
    var response =await http.post('doctor/login4Mobile',data: {
      'mobile':mobile,
      'verificationCode':vcode,
      'roleType':'doctor',
    },options: Options(contentType: Headers.jsonContentType));
    print("login->response=$response");
    return User.fromJsonMap(response.data);
  }

  //获取验证码
  static Future getVCode(String mobile) async{
    if(isTest){
      return getVCodeOfTest(mobile);
    }else{
      return getVCodeOfProduct(mobile);
    }
  }

  //获取验证码
  static Future getVCodeOfProduct(String mobile) async{
    print("getVCodeOfProduct->mobile=$mobile");
    var response =await http.get('doctor/sendSMS',queryParameters: {
      'mobile':mobile
    });
    print("getVCodeOfProduct->response=${response.data}");
    return true;
  }

  //获取验证码
  static Future getVCodeOfTest(String mobile) async{
    return Future.delayed(new Duration(milliseconds: 1000), ()
    {
      var data = {
        "code": 200,
        "msg": "success",
        "data": "{\"success\": true, \"id\": \"175562600756750081\"}"
      };
      ResponseData response = ResponseData.fromJson(data);
      return response.success;
    });
  }

  //获取验证码
  static Future modifyPassword(String mobile,String newPassword,String vCode) async{
    print("modifyPassword->mobile=$mobile,newPassword=$newPassword,vCode=$vCode");
    var response =await http.post('doctor/changePassword',data: {
      'mobile':mobile,
      'newPassword':newPassword,
      'confirmPassword':newPassword,
      'verificationCode':vCode,
      "roleType": "doctor"
    },options: Options(contentType: Headers.jsonContentType));
    print("modifyPassword->response=$response");

    return response;
  }








  // 查询医生管理的患者列表
  static Future fetchPatientListOfProduct(String token,{int page:1,int count:10})async {
    var response = await http.get('clinic/getPatients',queryParameters:{
      'page':page,
      'count':count,
    },options: Options(headers:{'token':token} ));
//    return response.data.map<Patient>((item) => Patient.fromJsonMap(item)).toList();

    return response.data;
  }

  static Future fetchPatientList(String token,{int page,int count:10})async{
    if(isTest){
      return fetchPatientListOfTest(token,page: page,count: count);
    }else{
      return fetchPatientListOfProduct(token,page: page,count: count);
    }
  }

  // 查询患者影像列表
  static Future fetchPatientPicListOfTest(String token,int patientId)async {
    return Future.delayed(new Duration(milliseconds: 2000), ()
    {
      var data = {
        "code": 200,
        "msg": "success",
        "data": [
          {
            "thumb": "https://cephimage.chinameyer.com/1/20200509/20200509093055654979.png?x-oss-process=image/resize,l_1000",
            "precision": 0.5,
            "name": "患者1的影像",
            "count": 2,
            "type": 1,
            "size":8203365,
            "compressFile": "http://121.196.200.204/uploads/dicoms.zip",
            "imageList": [
              "https://cephimage.chinameyer.com/1/20200509/20200509093055654979.png",
              "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598862615251&di=d7b59f4de29afd5d85ce251906fb324c&imgtype=0&src=http%3A%2F%2Fwxpic.7399.com%2FnqvaoZtoY6GlxJuvYKfWmZlkxaJhpc-bna9idJuZio%2Fabrr-KlnCrlpmXn3-jhrBoeGuoaJ96rXt3oGyEmKKqbmegyZ2riaSsbsR-oaiH3Z1uhJOEan3DcmiMdWSdkHaCgYi1ha2whXaEm4drpm7Ic6CYo2Nid9mxk5ugpG-io5yd.jpg",
              "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598862701986&di=57f69926bdb9fbe77bdaf2a584a33b8f&imgtype=0&src=http%3A%2F%2Ffile.cnkang.com%2Fcnkfile1%2FM00%2F03%2F50%2Fo4YBAFivBf6AJoLxAAFv459QA5Y29.jpeg",
              "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598862701985&di=17863d8f294137f0d91188ae30e10b4b&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2F1%2F22%2F7cb41452684.jpg",
              "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598862701983&di=bb89ed1882a42638614bc1382986dabf&imgtype=0&src=http%3A%2F%2Fpic.feizl.com%2Fupload%2Fallimg%2F170717%2F1710bhis5evxyvy.jpg",
              "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598862701983&di=a6ac6513675b6f0a2a1c88915f42ae09&imgtype=0&src=http%3A%2F%2Fpic.feizl.com%2Fupload%2Fallimg%2F170717%2F1710ecxy3o2uetk.jpg",
              "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1578364952,3440871037&fm=26&gp=0.jpg",
              "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598862701995&di=da5419012949b5f332c4ba9985f46a5c&imgtype=0&src=http%3A%2F%2Fpic.feizl.com%2Fupload%2Fallimg%2F170821%2F2135x0nxm1yd0vt.jpg",
              "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598862701995&di=97c6dd37063bc9935b2444167da27eb9&imgtype=0&src=http%3A%2F%2Fimg.tupianzj.com%2Fuploads%2Fallimg%2F160414%2F9-1604140T934.jpg",
              "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598862701994&di=bdca7859cc4b76d9af53cc9348f0744c&imgtype=0&src=http%3A%2F%2Fpic.feizl.com%2Fupload%2Fallimg%2F170614%2F1005195948-5.jpg",
              "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598862701993&di=c28628b9a151d8f5a471e70406b41016&imgtype=0&src=http%3A%2F%2Fpic.feizl.com%2Fupload%2Fallimg%2F180104%2F15984fu3y3u1g4pb.jpg",
            ]
          },
          {
            "thumb": "https://cephimage.chinameyer.com/1/20200509/20200509093055654979.png?x-oss-process=image/resize,l_1000",
            "precision": 0.5,
            "name": "患者2的影像",
            "count": 2,
            "type": 1,
            "compressFile": "https://codeload.github.com/kaina404/FlutterDouBan/zip/master-new",
            "imageList": [
              "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598862526047&di=98a29ab73367dbde7c647dbf73a63a55&imgtype=0&src=http%3A%2F%2Fimg.improve-yourmemory.com%2Fpic%2Fb9ec450127820b3af2ff611725fbb88d-0.jpg",
              "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598862526042&di=db659166b5e329d49b52bf4492547c59&imgtype=0&src=http%3A%2F%2Fztd00.photos.bdimg.com%2Fztd%2Fw%3D700%3Bq%3D50%2Fsign%3D9556a2318f18367aad897ddd1e48fae9%2F4ec2d5628535e5ddc216cac77fc6a7efce1b62eb.jpg"
            ]
          },
        ]
      };

      ResponseData response = ResponseData.fromJson(data);
      return response.data.map<MedicalImaging>((item) =>
          MedicalImaging.fromJsonMap(item)).toList();
    });
  }

  static Future fetchPatientPicList(String token,int patientId)async{
    if(isTest){
      return fetchPatientPicListOfTest(token,patientId);
    }else{
      return fetchPatientPicListOfProduct(token,patientId);
    }
  }

  // 查询患者影像列表
  static Future fetchPatientPicListOfProduct(String token,int patientId)async {
    var response = await http.get('doctor/scanImage4Mobile',queryParameters:{
      'patientId':patientId,
    },options: Options(headers:{'token':token} ));
    return response.data.map<MedicalImaging>((item) => MedicalImaging.parseJson(item)).toList();
  }

  static Future fetchPatientListOfTest(String token,{int page,int count:10}) async {
    return Future.delayed(new Duration(milliseconds: 1000), () {

      var pageData = {
        "code": 200,
      "msg": "success",
      "data": [
      {
      "id": 2,
      "avatar": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596013778401&di=a0cd8bbe4de9c87e6d8348c0bd9de890&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2F5%2Ffb%2Facc1823239.jpg",
      "patientNo": "20200731-A001",
      "username": "张三1",
      "sex": 0,
      "birthDate": "2020-07-29",
      "recordDate": "2020-07-30",
      "mobile": "15656557226",
      "address": "安徽省合肥市蜀山区高新经济开发区668号美亚光电A座1101",
      "ctNum": 0,
      "partCtNum": 3,
      "panoramicNum": 0,
      "lateralNum": 5,
      "tmjNum": 0,
      "doctorName": "",
      "clinicName": ""
      },
      {
      "id": 1,
      "avatar": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596014007185&di=5cf2599bbd7d23da8ef3d1820a0f94a7&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2Fa%2F1a%2Fdf7e1192187.jpg",
      "patientNo": "20200730",
      "username": "张三2",
      "sex": 0,
      "birthDate": "2020-07-29",
      "recordDate": "2020-07-30",
      "mobile": "13225855890",
      "address": "安徽省合肥市蜀山区高新经济开发区668号",
      "ctNum": 1,
      "partCtNum": 1,
      "panoramicNum": 2,
      "lateralNum": 3,
      "tmjNum": 4,
      "doctorName": "",
      "clinicName": ""
      },
        {
          "id": 3,
          "avatar": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596014007185&di=5cf2599bbd7d23da8ef3d1820a0f94a7&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2Fa%2F1a%2Fdf7e1192187.jpg",
          "patientNo": "20200730",
          "username": "张三3",
          "sex": 0,
          "birthDate": "2020-07-29",
          "recordDate": "2020-07-30",
          "mobile": "13225855890",
          "address": "安徽省合肥市蜀山区高新经济开发区668号",
          "ctNum": 1,
          "partCtNum": 1,
          "panoramicNum": 2,
          "lateralNum": 3,
          "tmjNum": 4,
          "doctorName": "",
          "clinicName": ""
        },
        {
          "id": 4,
          "avatar": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596014007185&di=5cf2599bbd7d23da8ef3d1820a0f94a7&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2Fa%2F1a%2Fdf7e1192187.jpg",
          "patientNo": "20200730",
          "username": "张三4",
          "sex": 0,
          "birthDate": "2020-07-29",
          "recordDate": "2020-07-30",
          "mobile": "13225855890",
          "address": "安徽省合肥市蜀山区高新经济开发区668号",
          "ctNum": 1,
          "partCtNum": 1,
          "panoramicNum": 2,
          "lateralNum": 3,
          "tmjNum": 4,
          "doctorName": "",
          "clinicName": ""
        },
        {
          "id": 5,
          "avatar": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596014007185&di=5cf2599bbd7d23da8ef3d1820a0f94a7&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2Fa%2F1a%2Fdf7e1192187.jpg",
          "patientNo": "20200730",
          "username": "张三5",
          "sex": 0,
          "birthDate": "2020-07-29",
          "recordDate": "2020-07-30",
          "mobile": "13225855890",
          "address": "安徽省合肥市蜀山区高新经济开发区668号",
          "ctNum": 1,
          "partCtNum": 1,
          "panoramicNum": 2,
          "lateralNum": 3,
          "tmjNum": 4,
          "doctorName": "",
          "clinicName": ""
        },
        {
          "id": 6,
          "avatar": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596014007185&di=5cf2599bbd7d23da8ef3d1820a0f94a7&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2Fa%2F1a%2Fdf7e1192187.jpg",
          "patientNo": "20200730",
          "username": "张三6",
          "sex": 0,
          "birthDate": "2020-07-29",
          "recordDate": "2020-07-30",
          "mobile": "13225855890",
          "address": "安徽省合肥市蜀山区高新经济开发区668号",
          "ctNum": 1,
          "partCtNum": 1,
          "panoramicNum": 2,
          "lateralNum": 3,
          "tmjNum": 4,
          "doctorName": "",
          "clinicName": ""
        },
        {
          "id": 7,
          "avatar": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596014007185&di=5cf2599bbd7d23da8ef3d1820a0f94a7&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2Fa%2F1a%2Fdf7e1192187.jpg",
          "patientNo": "20200730",
          "username": "张三7",
          "sex": 0,
          "birthDate": "2020-07-29",
          "recordDate": "2020-07-30",
          "mobile": "13225855890",
          "address": "安徽省合肥市蜀山区高新经济开发区668号",
          "ctNum": 1,
          "partCtNum": 1,
          "panoramicNum": 2,
          "lateralNum": 3,
          "tmjNum": 4,
          "doctorName": "",
          "clinicName": ""
        },
        {
          "id": 8,
          "avatar": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596014007185&di=5cf2599bbd7d23da8ef3d1820a0f94a7&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2Fa%2F1a%2Fdf7e1192187.jpg",
          "patientNo": "20200730",
          "username": "张三8",
          "sex": 0,
          "birthDate": "2020-07-29",
          "recordDate": "2020-07-30",
          "mobile": "13225855890",
          "address": "安徽省合肥市蜀山区高新经济开发区668号",
          "ctNum": 1,
          "partCtNum": 1,
          "panoramicNum": 2,
          "lateralNum": 3,
          "tmjNum": 4,
          "doctorName": "",
          "clinicName": ""
        },
        {
          "id": 9,
          "avatar": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596014007185&di=5cf2599bbd7d23da8ef3d1820a0f94a7&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2Fa%2F1a%2Fdf7e1192187.jpg",
          "patientNo": "20200730",
          "username": "张三9",
          "sex": 0,
          "birthDate": "2020-07-29",
          "recordDate": "2020-07-30",
          "mobile": "13225855890",
          "address": "安徽省合肥市蜀山区高新经济开发区668号",
          "ctNum": 1,
          "partCtNum": 1,
          "panoramicNum": 2,
          "lateralNum": 3,
          "tmjNum": 4,
          "doctorName": "",
          "clinicName": ""
        },
        {
          "id": 10,
          "avatar": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596014007185&di=5cf2599bbd7d23da8ef3d1820a0f94a7&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2Fa%2F1a%2Fdf7e1192187.jpg",
          "patientNo": "20200730",
          "username": "张三10",
          "sex": 0,
          "birthDate": "2020-07-29",
          "recordDate": "2020-07-30",
          "mobile": "13225855890",
          "address": "安徽省合肥市蜀山区高新经济开发区668号",
          "ctNum": 1,
          "partCtNum": 1,
          "panoramicNum": 2,
          "lateralNum": 3,
          "tmjNum": 4,
          "doctorName": "",
          "clinicName": ""
        },

      ],
      "total": 10
      };
//      var pageData = {
//      "code": 200,
//      "msg": "success",
//      "data": [],
//      };
      ResponseData response=ResponseData.fromJson(pageData);

      return response.data.map<Patient>((item) => Patient.fromJsonMap(item)).toList();
    });
  }


  static Future fetchImgDownloadUrl(String token,int patientId,String group)async {
    print("fetchImgDownloadUrl->token=$token,patientId=$patientId,group=$group");
    var response = await http.get('doctor/download4Zip',queryParameters:{
      'patientId':patientId,'serviceType':'android','group':group
    },options: Options(headers:{'token':token} ));
    print("fetchImgDownloadUrl->response=$response");
    return response.data;
  }
}